#include "wlan.h"


Wlan::Wlan(bool auto_connect)
{
    save_cfg.begin("Config");
    ssid = save_cfg.getString("ssid", "");
    password = save_cfg.getString("password", "");
    init_gui();
    WiFi.setSleep(true);
    if (auto_connect){
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid.c_str(),password.c_str());
    }
    else{
        
        if(WiFi.status()==WL_CONNECTED)
        {
            show_msg(LV_SYMBOL_WIFI, "Connected", WiFi.SSID());
            is_connected = true;      
        } 
        else{
            if (WiFi.getMode()==WIFI_STA)
            {
                WiFi.mode(WIFI_OFF);
            }
            show_msg(LV_SYMBOL_WARNING, "No", "Connection");
            is_connected = false;
        }
        timecal = millis()-5000;
    }
    
}

void Wlan::init_gui()
{
    label_scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(label_scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_scr_load(label_scr);

    lv_style_init(&font_style);
	lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    label_msg = lv_label_create(label_scr, NULL);
    label_sym = lv_label_create(label_scr, NULL);
    label_ssid = lv_label_create(label_scr, NULL);
	lv_obj_add_style(label_msg, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_sym, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_ssid, LV_LABEL_PART_MAIN, &font_style);
}

Event Wlan::main_process(Action act)
{
    event = NOEVENT;
    if(act==TURN_LEFT)
    {
        WiFi.disconnect();
        WiFi.mode(WIFI_OFF);
        show_msg(LV_SYMBOL_WARNING, "No", "Connection");
        is_connected = false;
        is_connecting = false;
    }
    if (!is_connected&&!is_connecting&&act==TURN_RIGHT)
    {
        WiFi.mode(WIFI_STA);
        show_msg(LV_SYMBOL_REFRESH, "Connecting...", "");
        lv_task_handler();
        is_connecting=true;
        try_connect();
        event = RGB_FLIKER;
    }
    else if(!is_connected&&is_connecting)
    {
        try_connect();
        event = RGB_FLIKER;
    }
    else if(is_connected)
    {
        event = GREEN;
    }
    else
    {
        event = RED;
    }
    return event;
}

void Wlan::try_connect()
{
    if (millis() - timecal>5000)
    {
        if(WiFi.status()==WL_CONNECTED)
        {
            show_msg(LV_SYMBOL_WIFI, "Connected", WiFi.SSID());
            is_connecting = false;
            is_connected = true;
        }
        else{
            WiFi.mode(WIFI_STA);
            WiFi.begin(ssid.c_str(),password.c_str());
        }
        timecal = millis();
    }
}

void Wlan::show_msg(String sym, String msg, String ssid)
{
    lv_label_set_text(label_msg, msg.c_str());
    lv_label_set_text(label_sym, sym.c_str());
    lv_label_set_text(label_ssid, ssid.c_str());
    lv_obj_align(label_msg, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align(label_sym, NULL, LV_ALIGN_CENTER, 0, -50);
    lv_obj_align(label_ssid, NULL, LV_ALIGN_CENTER, 0, 50);
    lv_task_handler();
}

Wlan::~Wlan()
{
    if(WiFi.status()!=WL_CONNECTED&&WiFi.getMode()==WIFI_STA)
    {
        WiFi.mode(WIFI_OFF);
    }
    lv_obj_del(label_scr);
}